#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "queue.h"

queue_t *QueueInit(const int max_size) {
	queue_t *pqueue = calloc(1, sizeof(*pqueue));
	assert(pqueue);
	pqueue->array = calloc(max_size, sizeof(*pqueue->array));
	assert(pqueue->array);
	pqueue->size = 0;
	pqueue->alloced_size = max_size;
}

void QueueDestroy(queue_t *pqueue) {
	assert(pqueue);
	assert(pqueue->array);
	free(pqueue->array);
	free(pqueue);
	pqueue = NULL;
}

void QueuePush(queue_t *pqueue, queue_elem_t elem) {
	assert(pqueue);
	assert(pqueue->array);
	assert(pqueue->size < pqueue->alloced_size);
	pqueue->array[pqueue->size++] = elem;
}

void PrintQueue(queue_t *pqueue) {
	assert(pqueue);
	assert(pqueue->array);
	int i = 0;
	for (i = 0; i < pqueue->size; ++i)
		printf("pqueue[%d] = [%d]\n", i, pqueue->array[i]);
}

queue_elem_t QueuePop(queue_t *pqueue) {
	assert(pqueue);
	assert(pqueue->array);
	assert(pqueue->size > 0);
	return pqueue->array[--pqueue->size];
	
}

queue_t *MakeSnapshot(queue_t *pqueue) {
	assert(pqueue);
	queue_t *ret = QueueInit(pqueue->alloced_size);
	ret->size = pqueue->size;
	unsigned int i = 0;
	for (; i < ret->size; ++i)
		ret->array[i] = pqueue->array[i];
	return ret;
}

